https://leetcode.cn/problems/top-k-frequent-words/
class Solution {
public:

    //建立小堆
    struct cmp
    {
        bool operator()(const pair<string,int>& a,const pair<string,int>& b )
        {
            if(a.second==b.second)
                return a.first<b.first;
            return a.second>b.second;
        }
    };

    vector<string> topKFrequent(vector<string>& words, int k) {
        //预处理
        unordered_map<string,int> hash;
        for(auto x:words)
            hash[x]++;

        priority_queue<pair<string,int>,vector<pair<string,int>>,cmp> heap;
        vector<string> ret(k);

        for(auto &x:hash)
        {
            heap.push(x);
            if(heap.size()>k)
                heap.pop();
        }

        //反着放
        for(int i=k-1;i>=0;i--)
        {
            ret[i]=heap.top().first;
            heap.pop();
        }
        return ret;
    }
};